package collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 * Linklist 的使用
 * 存储结构：双向列表
 * @author zff
 */
public class Demo06 {
    public static void main(String[] args) {
        //1.创建元素
        LinkedList<Object> linkedList = new LinkedList<>();
        Student s1 = new Student("张三",19);
        Student s2 = new Student("李四",20);
        Student s3 = new Student("王五",23);
        Student s4 = new Student("刘六",24);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        linkedList.add(s4);
        System.out.println(linkedList.toString());
        //删除元素
        System.out.println(linkedList.size());
        linkedList.remove(new Student("李四",20));//需要重写equals
        linkedList.remove("删除之后：" + linkedList.size());
//        linkedList.clear();
        //遍历元素
        //3.1for 循环
        System.out.println("=====使用for循环进行遍历=====");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        //3.2迭代器遍历
        System.out.println("=====使用迭代器进行遍历=====");
        Iterator<Object> it = linkedList.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        //3.3增强for循环
        System.out.println("使用增强for循环进行遍历");
        for (Object o : linkedList) {
            Student s = (Student)o;
            System.out.println(s.toString());
        }
        //3.4列表迭代器
        ListIterator<Object> lit = linkedList.listIterator();
        while(lit.hasNext()){
            System.out.println(lit.nextIndex()+ " :" + lit.next());
        }
        System.out.println("===============");
        while(lit.hasPrevious()){
            System.out.println(lit.previousIndex() + " : " + lit.previous());
        }
        //4.判断
        System.out.println(linkedList.contains(new Student("李四",20)));
        System.out.println(linkedList.isEmpty());
        //5.查找
        System.out.println(linkedList.indexOf(new Student("王五",23)));
    }
}
